package com.cnswhy.cloud.server.app.controller.spell;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cnswhy.cloud.common.core.mybatis.conditions.Wraps;
import com.cnswhy.cloud.server.app.controller.BaseController;
import com.cnswhy.cloud.server.core.entity.SpellPrice;
import com.cnswhy.cloud.server.core.query.QueryGenerator;
import com.cnswhy.cloud.server.core.service.spell.SpellPriceService;
import com.cnswhy.cloud.server.core.vo.RespVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/spellPrice")
@Slf4j
public class SpellPriceController extends BaseController {

    @Autowired
    private SpellPriceService spellPriceService;

    @RequestMapping("/ajaxSpellPriceProduct")
    @ResponseBody
    public RespVo ajaxSpellPriceProduct(SpellPrice bo) {
        log.info("拼购价格分区产品查询:{} {}", JSON.toJSONString(bo));

        RespVo vo = new RespVo();
        try {
            QueryWrapper<SpellPrice> queryWrapper = QueryGenerator.initQueryWrapper(bo);
            List<SpellPrice> listPriceProduct = spellPriceService.list(queryWrapper);
            vo.setCode(RespVo.CODE_SUCCESS);
            vo.setResult(listPriceProduct);
        } catch (Exception e) {
            vo.setCode(RespVo.CODE_ERROR);
            vo.setMessage("查询失败");
            log.error("查询失败", e);
        }
        return vo;
    }

    @RequestMapping("/listPrice")
    @ResponseBody
    public RespVo listPrice() {
        log.info("拼购价格分区查询");
        RespVo vo = new RespVo();
        try {

            vo.setCode(RespVo.CODE_SUCCESS);
            vo.setResult(spellPriceService.list(Wraps.<SpellPrice>lbQ().orderByAsc(SpellPrice::getSortOrder)));
        } catch (Exception e) {
            vo.setCode(RespVo.CODE_ERROR);
            vo.setMessage("查询失败");
            log.error("查询失败", e);
        }
        return vo;
    }
}
